package com.hiss.basic.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hiss.basic.client.system.ISysMenuService;
import com.hiss.basic.model.system.request.SysMenuCreateRequest;
import com.hiss.basic.model.system.request.SysMenuQueryRequest;
import com.hiss.basic.model.system.request.SysMenuUpdateRequest;
import com.hiss.basic.model.system.response.SysMenuQueryResponse;
import com.hiss.basic.model.system.response.route.RouteResponse;
import com.hiss.basic.starter.core.model.R;
import com.hiss.basic.starter.core.utils.tree.TreeNode;
import com.hiss.basic.starter.log.annotation.Log;
import com.hiss.basic.starter.log.enums.BusinessTypeEnum;
import com.hiss.basic.starter.security.helper.SecurityHelper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统菜单 controller
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Resource
    private ISysMenuService sysMenuService;

    @RequestMapping(value = "/routes", method = RequestMethod.GET)
    public R<List<RouteResponse>> routes() {
        Long userId = SecurityHelper.getUserId();
        List<RouteResponse> routes = sysMenuService.findRoutes(userId);
        return R.success(routes);
    }

    @SaCheckPermission("sys:menu:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<SysMenuQueryResponse>> list(SysMenuQueryRequest request) {
        List<SysMenuQueryResponse> deptList = sysMenuService.findList(request);
        return R.success(deptList);
    }

    @SaCheckPermission("sys:menu:list-tree")
    @RequestMapping(value = "/list/tree", method = RequestMethod.GET)
    public R<List<TreeNode<Long, Integer>>> listTree(SysMenuQueryRequest request) {
        List<TreeNode<Long, Integer>> menuList = sysMenuService.findTreeList(request);
        return R.success(menuList);
    }

    @SaCheckPermission("sys:menu:create")
    @Log(title = "新增菜单", businessType = BusinessTypeEnum.INSERT)
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public R<Void> create(@RequestBody SysMenuCreateRequest request) {
        sysMenuService.create(request);
        return R.success();
    }

    @SaCheckPermission("sys:menu:update")
    @Log(title = "更新菜单", businessType = BusinessTypeEnum.UPDATE)
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public R<Void> update(@RequestBody SysMenuUpdateRequest request) {
        sysMenuService.update(request);
        return R.success();
    }

    @SaCheckPermission("sys:menu:remove")
    @Log(title = "删除菜单", businessType = BusinessTypeEnum.DELETE)
    @RequestMapping(value = "remove", method = RequestMethod.DELETE)
    public R<Void> remove(Long id) {
        sysMenuService.remove(id);
        return R.success();
    }
}
